import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;

public class binarySearch {

    @Test
    public void test(){
        int [] array={14,1,3,56,15};
        System.out.println(binarySearch(array, 15));
    }
    public static int binarySearch(int[] array, int value) {
        //先排序
        //定义左边界和有边界
        int left=0;
        int right=array.length-1;
        while(left<=right){
            //定义中间值
//            int middle=(left+right)/2;
            int middle=left+(right-left)/2;//避免超出int的范围
            Arrays.sort(array);
            if (array[middle]==value){
                return middle;
            }else if (value<array[middle]){
                right=middle-1;
            }else if (value>array[middle]){
                left=middle+1;
            }
        }
        return -1;
    }
}
